package com.mrzhou.core.tool.utils;

import org.apache.commons.lang.StringEscapeUtils;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 项目生成一些数据工具类
 */
public class DataUtil {

    private static final String numStr = "1234567890";

    private static final String charStr = "123456789aAbBcCdDeEfFgGhHiIjJkKmMnNpPqQrRsStTuUvVwWxXyYzZ";

    public static String randomNum(int length) {
        return random(length, false);
    }

    public static String randomStr(int length) {
        return random(length, true);
    }

    public static String randomStr(int length, String randStr) {
        return random(length, true, randStr);
    }

    public static String random(int length, boolean isStr) {
        return random(length, isStr, StringUtil.EMPTY);
    }

    /**
     *
     * @param length 随机数的长度
     * @param isStr 为false时，只会取0~9的随机数, 此时randStr失效
     * @param randStr 如果randStr不为空且isStr=true时， 取randStr中的字符
     * @return
     */
    public static String random(int length, boolean isStr, String randStr) {
        String strTable = "";
        if (!isStr) {
            strTable = numStr;
        } else {
            strTable = StringUtil.isNotBlank(randStr) ? randStr  : charStr;
        }

        int len = strTable.length();
        String retStr = "";
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }

    /**
     * 过滤掉字符中的html标签
     * @param str
     * @return
     */
    public static String stringHtmlFilter(String str) {
        Matcher m_html = mathcherHtmlStr(str);
        // 转义下正常文字中的转义符
        return null == m_html ? null : StringEscapeUtils.unescapeHtml(m_html.replaceAll(""));
    }

    public static String getStrHtml(String str) {
        Matcher matcher = mathcherHtmlStr(str);
        if (matcher != null) {
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
        }
        return null;

    }

    public static Matcher mathcherHtmlStr(String str) {
        if(null == str) {
            return null;
        }

        // 去除所有的HTML标签
        String regEx = "(.*?<HEAD.*?>(.*?)<\\/HEAD>)|(<.*?>)|(<^/[a-zA-Z].*?>)";
        Pattern p_html = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        return p_html.matcher(str);
    }

    public static int randomInt(int min, int max) {
        return (int)(Math.random() * (max - min) + min);
    }

    public static String getUUID() {
        String str = UUID.randomUUID().toString();
        return str.replace("-", "");
    }

}
